package com.example.lovedogs.ui;


import com.example.lovedogs.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.SpannableStringBuilder;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

public class ExpandableTextView extends TextView {
    private static final int DEFAULT_TRIM_LENGTH = 200;//默认显示长度
    private static final String DEFAULR_ELLIPSIS = ".....";//默认结尾省略文字
 
    private CharSequence originalText;//原来的
    private CharSequence trimmedText;//休整过的
    private BufferType bufferType;
    private boolean trim = true;
    
    
    private int trimLength;//显示长度
    private String ellipsis_text;//结尾省略文字
 
    
    public void setOriginalText(CharSequence originalText) {
		this.originalText = originalText;
	}
    public boolean getTrim() {
    	System.out.println(trim);
		return trim;
	}

	public void setTrim(boolean trim) {
		this.trim = trim;
	}

	public ExpandableTextView(Context context) {
        this(context, null);
    }
 
    public ExpandableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
 
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ExpandableTextView);
        this.trimLength = typedArray.getInt(R.styleable.ExpandableTextView_trimLength, DEFAULT_TRIM_LENGTH);
        ellipsis_text = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.example.example_1015textview", "ellipsis_text");
        ellipsis_text=ellipsis_text==null?DEFAULR_ELLIPSIS:ellipsis_text;
        typedArray.recycle();
        
      /*  setText();
        requestFocusFromTouch();*/
        
        /*setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                trim = !trim;
                setText();
                requestFocusFromTouch();
            }
        });*/
    }
 
    public void setText() {
    	System.out.println("settext");
        super.setText(getDisplayableText(), bufferType);
    }
 
    private CharSequence getDisplayableText() {
    	CharSequence c= trim ? trimmedText : originalText;
        System.out.println(c);
        return c;
        
//        return trim ? trimmedText : originalText;
    }
 
    @Override
    public void setText(CharSequence text, BufferType type) {
        trimmedText = getTrimmedText(text);
        bufferType = type;
        setText();
    }
 
    private CharSequence getTrimmedText(CharSequence text) {
        if (originalText != null && originalText.length() > trimLength) {
            return new SpannableStringBuilder(originalText, 0, trimLength + 1).append(ellipsis_text);
        } else {
            return originalText;
        }
    }
 
    public CharSequence getOriginalText() {
        return originalText;
    }
 
    public void setTrimLength(int trimLength) {
        this.trimLength = trimLength;
        trimmedText = getTrimmedText(originalText);
        setText();
    }
 
    public int getTrimLength() {
        return trimLength;
    }
}